﻿using System.Collections.Generic;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.GlobalizationFramework.Domain;
using Fuse8.GlobalizationFramework.Domain.Commands;
using Fuse8.GlobalizationFramework.Infrastructure;

namespace Fuse8.GlobalizationFramework.BusinessServices
{
    /// <summary>
    /// Provides methods for administrative work with resources
    /// </summary>
    public static class AdminResourceService
    {
        private static readonly BusinessServiceFacade<Resource, int> businessService;

        static AdminResourceService()
        {
            Policy.InitDependicies();
            businessService = new BusinessServiceFacade<Resource, int>();
        }

        /// <summary>
        /// Gets all resources from data source. By default it adds default resources
        /// </summary>
        /// <param name="appendDefaultIfNo">Specifies if it is needed to append default resources if there is no with current culture</param>
        /// <returns></returns>
        public static IEnumerable<Resource> FindAll(bool appendDefaultIfNo = true)
        {
            var command = DependencyServiceLocator.Current.GetInstance<IResourceCommandFactory>().GetCommand<ResourceSelectAllByLocale>();
            GlobalizationMediator.InitCulture(command);
            if (appendDefaultIfNo)
            {
                command.AppendDefault = true;
                command.DefaultLocale =
                    DependencyServiceLocator.Current.GetInstance<IGlobalizationSetting>().DefaultCulture.Name;

            }

            return businessService.FindByCommand(command);
        }   
 
        /// <summary>
        /// Save resource
        /// </summary>
        /// <param name="resource">Resource to be saved</param>
        public static void Save(Resource resource)
        {
            businessService.Save(resource);
        }

        /// <summary>
        /// Remove resource
        /// </summary>
        /// <param name="id">Id of resource to be deleted</param>
        public static void Remove(int id)
        {
            businessService.Remove(id);
        }

        /// <summary>
        /// Find Resource by id
        /// </summary>
        /// <param name="id">Id of resource</param>
        /// <returns></returns>
        public static Resource FindOne (int id)
        {
            return businessService.FindOne(id);
        }


    }
}